package com.itheima.project.face.impl;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

import com.itheima.project.core.enums.search.SearchEnum;
import com.itheima.project.exception.ProjectException;
import com.itheima.project.face.DocumentFace;
import com.itheima.project.service.DocumentService;
import com.itheima.project.util.DelegateEsParamParser;
import com.itheima.project.vo.search.*;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * @author mrchen
 * @date 2022/3/25 15:32
 */
@Service
@Slf4j
public class DocumentFaceImpl implements DocumentFace {

    @Autowired
    private DocumentService documentService;

    @Autowired
    private NamedXContentRegistry namedXContentRegistry;

    @Override
    public <T> void save(T doc) throws ProjectException {
        documentService.save(doc);
    }

    @Override
    public void save(Map doc, String indexName) throws ProjectException {
        documentService.save(doc,indexName);
    }

    @Override
    public void saveBatch(List<Map> list, String indexName) throws ProjectException {
        documentService.saveBatch(list,indexName);
    }

    @Override
    public void deleteById(Object id, String indexName) {
        documentService.deleteById(id,indexName);
    }

    @Override
    public Object getById(Object id, String indexName) {
        return documentService.getById(id,indexName);
    }

    @Override
    public SearchResponse search(RequestParamVo paramVo, Class<?> tClass) {
        return documentService.search(DelegateEsParamParser.parseRequestParamVo(paramVo),tClass);
    }

    /**
     * 搜索方法
     * @param paramVo
     * @param indexName  根据指定索引库名称
     * @return
     */
    @Override
    public  SearchPageVo search(RequestParamVo paramVo, String indexName) {
        SearchResponse response = documentService.search(DelegateEsParamParser.parseRequestParamVo(paramVo),indexName);
        return DelegateEsParamParser.parseResponseResult(response,paramVo);
    }
    @Override
    public SearchPageVo<Object> search(String searchContent, String indexName) throws IOException {
        SearchSourceBuilder sourceBuilder = DelegateEsParamParser.parseSearchSourceBuilder(namedXContentRegistry, searchContent);
        SearchResponse searchResponse = documentService.search(sourceBuilder, indexName);
        RequestParamVo paramVo = RequestParamVo.builder()
                .pageParam(PageParam.of(sourceBuilder.from(), sourceBuilder.size()))
                .build();
        return DelegateEsParamParser.parseResponseResult(searchResponse,paramVo);
    }
    @Override
    public String searchNativeResult(String searchContent, String indexName) throws IOException {
        SearchSourceBuilder sourceBuilder = DelegateEsParamParser.parseSearchSourceBuilder(namedXContentRegistry, searchContent);
        SearchResponse searchResponse = documentService.search(sourceBuilder, indexName);
        return Strings.toString(searchResponse);
    }
}
